package com.inspur.edp.formserver.vmmanager.lowcode;

import com.inspur.edp.cef.entity.config.CefConfig;
import com.inspur.edp.formserver.viewmodel.GspViewModel;
import com.inspur.edp.formserver.viewmodel.extendinfo.entity.GspVoExtendInfo;
import com.inspur.edp.formserver.viewmodel.extendinfo.entity.VoConfigCollectionInfo;
import com.inspur.edp.formserver.voextendinfo.server.api.GspVoExtendInfoRpcService;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.MetadataPackage;
import com.inspur.edp.lcm.metadata.api.entity.ProcessMode;
import com.inspur.edp.metadata.rtcustomization.serverapi.CustomizationServerService;
import com.inspur.edp.metadata.rtcustomization.spi.event.ExtMdSavedArgs;
import com.inspur.edp.metadata.rtcustomization.spi.event.IMetadataDeployEventListener;
import com.inspur.edp.metadata.rtcustomization.spi.event.MdPkgDeployedEventArgs;
import io.iec.edp.caf.boot.context.CAFContext;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.core.context.ICAFContextService;
import io.iec.edp.caf.core.session.CafSession;
import io.iec.edp.caf.core.session.ICafSessionService;
import io.iec.edp.caf.core.session.SessionType;
import io.iec.edp.caf.core.session.core.CAFSessionThreadHolder;
import io.iec.edp.caf.tenancy.api.ITenantService;
import io.iec.edp.caf.tenancy.api.entity.Tenant;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

public class MdPkgToolsDeployListener implements IMetadataDeployEventListener {

// CustomizationServerService metadataService;

  public MdPkgToolsDeployListener() {
//    this.metadataService = SpringBeanUtils.getBean(CustomizationServerService.class);
  }

  @Override
  public void fireMdPkgDeployedEvent(MdPkgDeployedEventArgs mdPkgDeployedEventArgs) {
//    if(metadataService == null) {
//      this.metadataService = SpringBeanUtils.getBean(CustomizationServerService.class);
//    }
//    MetadataPackage metadataPackage = mdPkgDeployedEventArgs.getPcakage();
//    handleMdPkg(metadataPackage, var -> metadataService.getMetadata(var));
  }

  @Override
  public void fireExtMdSavedEvent(ExtMdSavedArgs extMdSavedArgs) {
//    if(metadataService == null) {
//      this.metadataService = SpringBeanUtils.getBean(CustomizationServerService.class);
//    }
  }
//  void handleMdPkg(MetadataPackage metadataPackage,
//      Function<String, GspMetadata> getMetadata) {
//    try {
////      if (metadataPackage.getHeader().getProcessMode() != ProcessMode.interpretation) {
////        //如果不是无需生成代码的元数据包，直接忽略
////        return;
////      }
//
//      List<GspMetadata> packageMetadataList = metadataPackage.getMetadataList();
//      if (packageMetadataList == null || packageMetadataList.isEmpty()) {
//        //如果元数据包下不存在元数据
//        return;
//      }
//      List<GspMetadata> metadatas = packageMetadataList.stream()
//          .filter(item -> item.getHeader().getType().equals("GSPViewModel"))
//          .collect(Collectors.toList());
//      if (metadatas.isEmpty()) {
//        return;
//      }
//
//      BSessionUtil.wrapFirstTenantBSession(tenant -> {
//        //获取当前已部署的eapi版本，并移除未变动的Eapi元数据
//        GspVoExtendInfoRpcService beInfoService = SpringBeanUtils
//            .getBean(GspVoExtendInfoRpcService.class);
//
//
//        List<GspVoExtendInfo> infos = new ArrayList(metadatas.size());
//        ProcessMode processMode = Optional.ofNullable(metadataPackage.getHeader().getProcessMode()).orElse(ProcessMode.generation);
//        for (GspMetadata metadata : metadatas) {
//          GspViewModel be = (GspViewModel) getMetadata.apply(metadata.getHeader().getId())
//              .getContent();
//          if (be.getGeneratedConfigID() == null) {
//            continue;
//          }
//
//         // GspVoExtendInfo existing = beInfoService.getVoExtendInfo(be.getID());
//          GspVoExtendInfoRpcService rpcService= SpringBeanUtils.getBean(GspVoExtendInfoRpcService.class);
//          GspVoExtendInfo existing = rpcService.getVoExtendInfo(be.getID());
//
//          //if (existing == null || !be.getGeneratedConfigID().equals(existing.getConfigId())) {
//            GspVoExtendInfo info = buildVoExtendInfo(processMode, be, existing);
//
//            infos.add(info);
//
//        }
//        if (!infos.isEmpty()) {
//          beInfoService.saveGspVoExtendInfos(infos);
//        }
//      });
//    } catch (Exception e) {
//      System.out.println("部署解析VO元数据出错：" + e.getMessage());
//
//    }
//  }

//  private GspVoExtendInfo buildVoExtendInfo(ProcessMode processMode, GspViewModel be, GspVoExtendInfo existing) {
//    GspVoExtendInfo info = new GspVoExtendInfo();
//    VoConfigCollectionInfo voConfigCollectionInfo = getBffConfigCollectionInfo(be);
//    voConfigCollectionInfo.setProjectType(processMode);
//    info.setVoConfigCollectionInfo(voConfigCollectionInfo);
//    info.setId(be.getID());
//    info.setConfigId(be.getGeneratedConfigID());;
//    info.setCreatedOn(Date.from(CAFContext.current.getCurrentDateTime().toInstant()));
//    info.setLastChangedOn(Date.from(CAFContext.current.getCurrentDateTime().toInstant()));
//    if (!be.getIsVirtual()) {
//      info.setBeSourceId(be.getMapping().getTargetMetadataId());
//    }
//    if (existing != null) {
//      info.setLastChangedOn(Date.from(CAFContext.current.getCurrentDateTime().toInstant()));
//    }
//    return info;
//  }
//
//  private VoConfigCollectionInfo getBffConfigCollectionInfo(GspViewModel vo) {
//    CefConfig cefConfig = new CefConfig();
//    cefConfig.setID(vo.getGeneratedConfigID());
//    cefConfig.setDefaultNamespace(vo.getCoreAssemblyInfo().getDefaultNamespace().toLowerCase());
//    VoConfigCollectionInfo voConfigCollectionInfo = new VoConfigCollectionInfo();
//    voConfigCollectionInfo.setConfig(cefConfig);
//    return voConfigCollectionInfo;
//  }

}
